<template>
  <!--    <el-form v-model="dialogVisible" width="80%" title="操作步骤" destroy-on-close center>-->
    <el-form width="80%" title="BOM组成" >
        <template #default>
            <!-- 列表部分开始 -->
            <el-row style="margin-top: 10px;">
                <el-col :span="24">
                    <el-table ref="BOMTable" :data="BOMs" border :stripe="true" style="width: 100%">
                        <el-table-column type="index" width="55" label="序号" />
                        <el-table-column label="BOM编号" >
                            <template #default="scope">
                                <span v-if="scope.row.category == 1">{{ scope.row.proId }}</span>
                                <span v-else>{{ scope.row.matId }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="BOM名称">
                            <template #default="scope">
                                <span v-if="scope.row.category == 1">{{ scope.row.proName }}</span>
                                <span v-else>{{ scope.row.matName }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="规格型号">
                            <template #default="scope">
                                <span v-if="scope.row.category == 1">{{ scope.row.proModel }}</span>
                                <span v-else>{{ scope.row.matModel }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="单位" >
                            <template #default="scope">
                                <span v-if="scope.row.category == 1">{{ scope.row.proUnit }}</span>
                                <span v-else>{{ scope.row.matUnit }}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="category" label="物料/产品">
                            <template #default="scope">
                                <span v-if="scope.row.category == 1">产品</span>
                                <span v-else>物料</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="bomUsage" label="预计使用量" />
                        <el-table-column label="操作" width="140">
                            <template #default="scope">
                                <el-button type="primary" @click="toAddOrder(scope.row)" size="small">生成工单</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>
            <!-- 列表部分结束 -->
            <!-- 分页部分开始 -->
            <el-row style="margin-top: 10px;">
                <el-col :span="24">
                    <el-pagination background @change="pageChange" layout="prev, pager, next" v-bind:page-size="limit" :total="total" />
                </el-col>
            </el-row>
            <!-- 分页部分结束 -->
            <OrderBOMAddWin ref="addOrder" :visible="addBOMVisible" :bomId="bomId" :woId="woId" @optionCompleted="addBOMOptionCompleted"/>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="20"></el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="closeWin">关闭</el-button>
                </el-col>
            </el-row>
        </template>
    </el-form>
</template>

<script>
import {watch} from "vue";
import OrderBOMRequest from '../../../../api/orderBOM.js';
import OrderBOMAddWin from "@/views/pro/order/BOM/orderBOM-add-win.vue";


export default {
    props:["woId","bomId"],// 属性
    emits:["optionCompleted"],// 暴漏的事件
    name: "BOM.vue",
    components:{
        OrderBOMAddWin
    },
    data(){//数据模型
        return{
            order:{},//接收BOM数据
            dialogVisible: false, // 弹窗标记
            queryBOM:{},// 绑定查询条件
            BOMs:[],
            total:0,// 总记录数
            limit:10,// 每页显示的记录
            currentPage:1,// 当前页码
            currentStepType:undefined,
            woId:this.woId,
            bomId:"",
            addBOMVisible:false,//生成工单弹窗

        }
    },methods:{
        addBOMOptionCompleted(){
          this.addBOMVisible = false;
        },
        /**
         * 翻页事件
         */
        pageChange(currentPage,currentLimit){

        },

        /**
         * 关闭当前的窗体
         */
        closeWin(){
            // 通知父窗体，操作完成
            this.$emit("optionCompleted");
        },
        /**
         * 加载BOM组成列表
         */
        loadBOM(woId){
            // 准备参数
            this.queryBOM.page = this.currentPage;
            this.queryBOM.limit = this.limit;
            if(woId)
                this.queryBOM.woId = woId;
            OrderBOMRequest.loadOrderBOM(this.queryBOM,res => {
                console.log(res)
                // 赋值
                this.BOMs = res.data.data;
                this.total = res.data.total;
            });
        },
        toAddOrder(row){
            this.$refs.addOrder.setParentId(row.woId);
            this.bomId = row.bomId
            this.addBOMVisible = true;
            console.log(this.bomId)
        }
    },mounted(props) {
        // this.loadBOM();
        console.log('加载的生产工单')
        console.log(this.woId)
        this.loadBOM(this.woId);
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
            if(this.dialogVisible){
                this.loadBOM(this.woId);
            }
        });
        watch(()=>this.woId,(newVal,oldVal)=>{
            console.log(this.woId)
            this.currentStepType = newVal;
            if(this.dialogVisible){
                this.loadBOM(this.woId);
            }
        });

    }
}
</script>

<style scoped>

</style>
